package com.burntsky.xbmc.android;


import java.util.ArrayList;

import org.xbmc.api.object.TvShow;

import android.app.Activity;
import android.app.ListFragment;
import android.content.ClipData;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class ActivitySelectionListFragment extends ListFragment {

	private XbmcController _Controller;
	private HomeActivity _categoryListener;
	private ArrayList<Category> _categoryCache;
	
	public ActivitySelectionListFragment(XbmcController controller){
		this._Controller = controller;
	}
	
	/* (non-Javadoc)
	 * @see android.app.ListFragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		this.populateCategories();
      
        ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setCacheColorHint(Color.TRANSPARENT);
        /*
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
                final String title = (String) ((TextView) v).getText();

                // Set up clip data with the category||entry_id format.
                final String textData = String.format("%d||%d", mCategory, pos);
                ClipData data = ClipData.newPlainText(title, textData);
                v.startDrag(data, new MyDragShadowBuilder(v), null, 0);
                return true;
            }
        });*/

	}
	
	private void populateCategories(){
		ArrayList<Category> cats = CategoryManager.GetCategories();
        //DirectoryCategory cat = Directory.getCategory(category);
        String[] items = new String[cats.size()];
        for (int i = 0; i < cats.size(); i++)
            items[i] = cats.get(i).getName();
        setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.title_list_item, items));
  
        this._categoryCache = cats;
	}

	/* (non-Javadoc)
	 * @see android.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		if(this._categoryListener != null)
		{
			//TvShow show = ActivitySelectionListFragment._ShowCache.get(position);
			//this._listener(show.title);
			Category category = this._categoryCache.get(position);
			this._categoryListener.ShowCategory(category);
		}
		
		//this.getActivity().getActionBar().hide();
	}

	
    @Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		//this._listener = (OnTvShowSelectedListener)activity;
		try{
			this._categoryListener = (HomeActivity)activity;			
		}
		catch(Exception ex){
			Toast.makeText(getActivity(), "invalid cast", Toast.LENGTH_LONG);
		}
	}


	public interface OnTvShowSelectedListener {
        public void onTvShowSelected(String tvShowName);
    }
}
